package com.ikingtech.platform.service.system.role.service;

import com.ikingtech.framework.sdk.component.api.CompRoleApi;
import com.ikingtech.framework.sdk.component.model.ComponentRole;
import com.ikingtech.framework.sdk.context.security.Me;
import com.ikingtech.framework.sdk.enums.component.PickerElementTypeEnum;
import com.ikingtech.framework.sdk.role.model.RoleDTO;
import com.ikingtech.framework.sdk.utils.Tools;
import lombok.RequiredArgsConstructor;

import java.util.List;

/**
 * @author tie yan
 */
@RequiredArgsConstructor
public abstract class AbstractCompRoleService implements CompRoleApi {

    private final RoleRepository service;

    @Override
    public List<ComponentRole> listByName(String name) {
        return this.convert(this.service.modelConvert(this.service.listByName(name, Me.domainCode(), Me.tenantCode(), Me.appCode())));
    }

    private List<ComponentRole> convert(List<RoleDTO> roles) {
        return Tools.Coll.convertList(roles, role -> {
            ComponentRole compRole = new ComponentRole();
            compRole.setElementId(role.getId());
            compRole.setElementName(role.getName());
            compRole.setElementType(PickerElementTypeEnum.ROLE);
            compRole.setTenantCode(role.getTenantCode());
            return compRole;
        });
    }
}
